/**
 * 运费模板
 */
import React, {Component} from 'react';
import 'antd/dist/antd.css';
import '../../css/transportTemplate.css';
import {goods, requestUrl, template, china} from '../../value';
import app from '../../app';
import {config} from '../../config';
import {
    Button,
    Form,
    Input,
    Radio,
    Checkbox,
    Row,
    Col,
    InputNumber,
    Select,
    Spin,
    message,
    Modal,
    Table,
    Pagination
} from 'antd';
import moment from 'moment';
import reqwest from 'reqwest';

const path = config.path;
const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;
const CheckboxGroup = Checkbox.Group;
const confirm = Modal.confirm;

const pageLength = 3;

class TransportTemplate extends Component {
    constructor(props) {
        super(props);
        this.state = {
            agreeLoading: false,
            disagreeLoading: false,
            provinceList: [],
            data: [],
            pagination: {
                current: 1,
                total: 1,
                pageSize: 10
            },
            loading: false,
            isAddValid: false, //是否弹出添加框
            spinning: false,
            flag: '2',
            allArea: [],
        }
        ;
    }

    formItemLayout = {
        labelCol: {span: 4},
        wrapperCol: {span: 18},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        return (
            <span>
                <Row>
                    <Row className="titleSize btnBottom">运费模板设置
                            <Button type="primary" className="refresh" style={{marginLeft: 30}}
                                    onClick={() => this.handleRefresh()}>刷新</Button>
                    </Row>
                    <Row className='btnBottom' style={{marginTop: 15, marginBottom: 15}}>
                        <Button onClick={() => {
                            this.handleReturnPage()
                        }} type="primary">添加运费模板</Button>
                    </Row>

                    <Spin spinning={this.state.spinning}>
                         <Row>
                        {
                            this.state.data.map((item, index) => {
                                return (
                                    <Col span={20} key={index} style={{marginBottom: '30px'}}>

                                        {/*模板名称  类似第一行表头*/}
                                        <Row className='title'>
                                            <Col span={20}>{item.template_name}</Col>
                                            <Col span={4}>
                                                <Button type='primary' className='btnRight' onClick={(items) => {
                                                    this.handleEditTemplate(item)
                                                }}>修改</Button>

                                                <Button type='danger' onClick={() => {
                                                    this.handleDeleteTemplate(item.template_id)
                                                }}>删除</Button>
                                            </Col>
                                        </Row>

                                        {/*第二行列表的名称*/}
                                        <Row style={{fontWeight: 'bold'}}>
                                            <Col className='content' span={4}>运送方式</Col>
                                            <Col className='content' span={4}>运送到</Col>

                                            <Col className='content'
                                                 style={{display: item.template_shipping === '1' ? 'block' : 'none'}}
                                                 span={4}>首件(件)</Col>
                                            <Col className='content'
                                                 style={{display: item.template_type === '0' ? 'block' : 'none'}}
                                                 span={4}>首件(件)</Col>
                                            <Col className='content'
                                                 style={{display: item.template_type === '1' ? 'block' : 'none'}}
                                                 span={4}>首重(kg)</Col>
                                            <Col className='content'
                                                 style={{display: item.template_type === '2' ? 'block' : 'none'}}
                                                 span={4}>首体积(m3)</Col>

                                            <Col className='content' style={{borderRight: 'none', borderTop: 'none'}}
                                                 span={4}>首费(元)</Col>

                                            <Col className='content' style={{
                                                display: item.template_shipping === '1' ? 'block' : 'none',
                                                borderRight: 'none',
                                                borderTop: 'none'
                                            }} span={4}>续件(件)</Col>
                                            <Col className='content' style={{
                                                display: item.template_type === '0' ? 'block' : 'none',
                                                borderRight: 'none',
                                                borderTop: 'none'
                                            }} span={4}>续件(件)</Col>
                                            <Col className='content' style={{
                                                display: item.template_type === '1' ? 'block' : 'none',
                                                borderRight: 'none',
                                                borderTop: 'none'
                                            }} span={4}>续重(kg)</Col>
                                            <Col className='content' style={{
                                                display: item.template_type === '2' ? 'block' : 'none',
                                                borderRight: 'none',
                                                borderTop: 'none'
                                            }} span={4}>续体积(m3)</Col>

                                            <Col className='content' span={4}>续费(元)</Col>
                                        </Row>
                                        <Row>
                                            <Col span={24}>
                                                {/*运送方式*/}
                                                <Col className='content'
                                                     span={4}>{item.template_method == 0 ? '快递' : ''}{item.template_method == 1 ? 'EMS' : ''}{item.template_method == 2 ? '平邮' : ''}{!item.template_method ? '默认' : ''}</Col>

                                                {/*运送到哪个地区  没有指定地区的运费*/}
                                                <Col className='content'
                                                     span={4}>{item.areaList.length == 0 ? '全国' : '其他'}</Col>

                                                {/*首件*/}
                                                <Col className='content' span={4}>{item.template_firstNumber || 0}</Col>

                                                {/*首费*/}
                                                <Col className='content' span={4}>{item.template_firstPrice || 0}</Col>

                                                {/*续件*/}
                                                <Col className='content' span={4}>{item.template_lastNumber || 0}</Col>

                                                {/*续费*/}
                                                <Col className='content' span={4}>{item.template_lastPrice || 0}</Col>

                                            </Col>

                                            {/*指定地区的运费*/}
                                            {
                                                item.areaList.map((areaItem, areaIndex) => {
                                                    if (areaIndex === 0) {
                                                        let method;
                                                        let province = [];
                                                        if (item.template_method === '1') {
                                                            method = '快递';
                                                        } else if (item.template_method === '2') {
                                                            method = 'EMS';
                                                        } else {
                                                            method = '平邮';
                                                        }
                                                        let newList = item.areaList.map((areaItem) => {
                                                            return areaItem;
                                                        });
                                                        let key = item.areaList[item.areaList.length - 1].template_area_key;
                                                        for (let i = 1; i <= key; i++) {
                                                            let list = {
                                                                province: '',
                                                            };

                                                            item.areaList.map((areaItem) => {
                                                                if (i == areaItem.template_area_key) {
                                                                    let chinese = '';
                                                                    list.template_area_key = areaItem.template_area_key;
                                                                    list.province += areaItem.template_area_content + ',';

                                                                    /*遍历求出中文名*/
                                                                    let idList = list.province.substring(0, 27).split(",");
                                                                    this.state.allArea.forEach((item) => {
                                                                        idList.forEach((items) => {
                                                                            if (items == item.id) {
                                                                                chinese += item.name + ',';
                                                                            }
                                                                        })
                                                                    });
                                                                    chinese = chinese.substring(0, chinese.length - 1);

                                                                    list.chineseName = chinese;
                                                                    list.template_area_firstNumber = areaItem.template_area_firstNumber;
                                                                    list.template_area_firstPrice = areaItem.template_area_firstPrice;
                                                                    list.template_area_lastNumber = areaItem.template_area_lastNumber;
                                                                    list.template_area_lastPrice = areaItem.template_area_lastPrice;
                                                                }
                                                            });
                                                            list.province = list.province.substring(0, list.province.length - 1);
                                                            province.push(list);
                                                        }
                                                        let provinceHtml = province.map((provinceItem, provinceIndex) => {


                                                            return (
                                                                <Col span={24}
                                                                     key={index + "-" + areaIndex + "-" + provinceIndex}>
                                                                    <Col className='content' span={4}>{method}</Col>
                                                                    <Col className='content'
                                                                         span={4}>{provinceItem.chineseName}...</Col>
                                                                    <Col className='content'
                                                                         span={4}>{provinceItem.template_area_firstNumber}</Col>
                                                                    <Col className='content'
                                                                         span={4}>{provinceItem.template_area_firstPrice}</Col>
                                                                    <Col className='content'
                                                                         span={4}>{provinceItem.template_area_lastNumber}</Col>
                                                                    <Col className='content'
                                                                         span={4}>{provinceItem.template_area_lastPrice}</Col>
                                                                </Col>
                                                            )
                                                        });
                                                        return provinceHtml;
                                                    }
                                                })
                                            }
                                        </Row>
                                    </Col>
                                )
                            })
                        }
                    </Row>
                    </Spin>

                    <Row style={{float: 'right'}}>
                        <Col>
                            <Pagination
                                current={this.state.pagination.current}
                                pagination={this.state.pagination}
                                total={this.state.pagination.total}
                                pageSize={this.state.pagination.pageSize}
                                onChange={this.handleTableChange}
                                loading={this.state.loading}/>
                        </Col>
                    </Row>
                </Row>
            </span>
        );
    }

    componentDidMount() {
        /*获取完省市区后再执行fetch函数*/
        this.handleSearchAllCity().then(() => {
            const param = {
                page: 1,
                size: 10
            };
            this.fetch(param);
        });

    }

    /*查看所有的省市区*/
    handleSearchAllCity() {
        return app.post(china.SEARCH_ADDRESS_LIST, {}).then((req) => {
            const _this = this;
            if (req.code == 1) {
                let area = _this.state.allArea;
                req.data.forEach((item) => {
                    item.sub.forEach((items) => {
                        items.sub.forEach((itemArea) => {
                            area.push(itemArea)
                        })
                    })
                });
                _this.state.allArea = area;
                _this.setState({allArea: area});
            }
        })
    }

    // 点击编辑按钮跳转页面
    handleEditTemplate(data) {
        app.setData('editTransport', data);
        this.props.history.push(path + '/home/editTransport'); //跳转到添加运费模板界面
    }

    // 子组件提交编辑数据
    handleGetConfirmEdit(value) {
        app.post(goods.UPDATE_TEMPLATE, value).then(req => {
            if (req.code == 1) {
                this.fetch();
                this.setState({
                    flag: '1'
                });
                app.alert('修改成功', 'success', 3);
            } else if (req.code == 2) {
                app.alert(req.msg, 'error', 3)
            }
        })
    }


    isEmptyObject(filters) {
        for (let key in filters) {
            if (key) {
                this.setState({param: filters});
                return filters;
            }
        }
        return null;
    }


    handleReturnPage() {
        this.props.history.push(path + '/home/addTransport'); //跳转到添加运费模板界面
    }

    handleTableChange = (pagination, filters, sorter) => {
        filters = this.isEmptyObject(filters);
        if (filters) {
            this.state.param = filters;
        }
        const pager = {...this.state.pagination};
        pager.current = pagination;
        this.setState({
            pagination: pager,
        });
        this.fetch({
            startList: pagination,
            ...this.state.param,
        });
    };

    fetch = (params = {}) => {
        this.setState({spinning: true});
        reqwest({
            url: app.serverUrl + template.SEARCH_TEMPLATE_LIST,
            method: 'post',
            data: {
                token: app.getToken(),
                ...params,
            },
            type: 'json',
        }).then((data) => {
            this.setState({
                spinning: false
            });
            if (data.code == 1) {
                const pagination = {
                    current: params.page,
                    pageSize: params.size,
                    total: data.count
                };
                for (let i = 0; i < data.data.length; i++) {
                    if (!data.data[i].areaList) {
                        data.data[i].areaList = [];
                    }
                }
                this.setState({
                    loading: false,
                    count: data.count,
                    data: data.data || [],
                    pagination,
                    flag: '2'
                });
            } else {
                app.alert(data.msg, 'info', 1);
                data:[]
            }
        });
    };

    // 删除模板
    handleDeleteTemplate(templateId) {
        const _this = this;
        confirm(
            {
                title: '',
                content: '您确定要删除此运费模板吗？',
                onOk() {
                    app.post(template.DEL_TEMPLATE, {
                        template_id: templateId,
                    }).then(req => {
                        if (req.code == 1) {
                            app.alert('删除成功', 'success', 1);
                            const param={
                                page:1,
                                size:10
                            };
                            _this.fetch(param);
                        } else if (req.code == 2) {
                            app.alert(req.msg, 'info', 1)
                        }
                    })
                },
                onCancel() {
                },
            });
    }


//刷新当前页面
    handleRefresh = () => {
        this.fetch({
            startList: this.state.pagination.current,
            ...this.state.param,
        });
    }
}

// 编辑运费模板
class EditTransport extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isVisible: false,
            confirmLoading: false,
            provinceLists: this.props.provinceList || [],
            first: '',
            addOrigin: '',

            disabledCost: [],  //指定地区中禁止的省和对应的运费
        };
    }

    formItemLayout = {
        labelCol: {span: 8},
        wrapperCol: {span: 13},
    };

    formDistrictLayout = {
        labelCol: {span: 6},
        wrapperCol: {span: 18},
    };

    render() {
        const {getFieldDecorator} = this.props.form;
        // 包邮
        if (this.props.data.template_shipping == '1') {
            return (
                <span style={{paddingLeft: 10, paddingRight: 10}}>
                <Button type="primary" onClick={() => this.setModal2Visible(true)}>修改</Button>
                <Modal
                    maskClosable={false}
                    title='修改运费模板'
                    wrapClassName="vertical-center-modal"
                    width='600px'
                    confirmLoading={this.state.confirmLoading}
                    visible={this.state.isVisible}
                    onCancel={() => this.setModal2Visible(false)}
                    onOk={() => this.confirmEdit(true, 1)}
                >
                    <Row>
                            <Col offset={1} span={18}>
                              <Form className="login-form">
                                                     <FormItem label={"模板名称"} {...this.formItemLayout}>
                                        {getFieldDecorator('template_name', {
                                            rules: [{required: true, message: '请输入模板名称!'}],
                                        })(
                                            <Input placeholder="请输入"/>
                                        )}
                                    </FormItem>
                                    <FormItem label={"发货时间:"} {...this.formItemLayout}>
                                        {getFieldDecorator('template_time', {
                                            rules: [{required: true, message: '请选择发货时间!'}],
                                        })(
                                            <Select style={{width: '200px'}}>
                                                <Option value="12">12小时</Option>
                                                <Option value="24">24小时</Option>
                                                <Option value="48">48小时</Option>
                                            </Select>
                                        )}
                                    </FormItem>
                                </Form>
                            </Col>
                        </Row>
                </Modal>
            </span>
            )
        }

        // 默认运费
        else {
            // 标题上的修改  统一修改运送方式和计价方式
            if (this.props.others == '0') {
                return (
                    <span style={{paddingLeft: 10, paddingRight: 10}}>
                <Button type="primary" onClick={() => this.setModal2Visible(true)}>修改</Button>
                <Modal
                    maskClosable={false}
                    title='修改运费模板'
                    wrapClassName="vertical-center-modal"
                    width='700px'
                    confirmLoading={this.state.confirmLoading}
                    visible={this.state.isVisible}
                    onCancel={() => this.setModal2Visible(false)}
                    onOk={() => this.confirmEdit(true, 2)}
                >
                    <Row>
                            <Col offset={2} span={18}>
                              <Form className="login-form">
                                                     <FormItem label={"模板名称"} {...this.formItemLayout}>
                                        {getFieldDecorator('template_name', {
                                            rules: [{required: true, message: '请输入模板名称!'}],
                                        })(
                                            <Input placeholder="请输入"/>
                                        )}
                                    </FormItem>
                                    <FormItem label={"发货时间:"} {...this.formItemLayout}>
                                        {getFieldDecorator('template_time', {
                                            rules: [{required: true, message: '请选择发货时间!'}],
                                        })(
                                            <Select style={{width: '200px'}}>
                                                <Option value="12">12小时</Option>
                                                <Option value="24">24小时</Option>
                                                <Option value="48">48小时</Option>
                                            </Select>
                                        )}
                                    </FormItem>
                                  {/*运送方式*/}
                                  <FormItem label={"运送方式"}
                                            {...this.formItemLayout}>
                                    {getFieldDecorator('template_method', {
                                        rules: [{required: true, message: '请选择运送方式!'}],
                                    })(
                                        <RadioGroup>
                                            <Radio value={'0'}>快递</Radio>
                                            <Radio value={'1'}>EMS</Radio>
                                            <Radio value={'2'}>平邮</Radio>
                                        </RadioGroup>
                                    )}
                                </FormItem>
                                  {/*根据运送方式选择计价方式*/}
                                  <FormItem style={{backgroundColor: '#ddd'}}
                                            label={"请先选择计价方式"} {...this.formItemLayout}>
                                    {getFieldDecorator('template_type', {
                                        rules: [{required: true, message: '请选择计价方式!'}],
                                    })(
                                        <RadioGroup>
                                            <Radio value={'0'}>按件数</Radio>
                                            <Radio value={'1'}>按重量</Radio>
                                            <Radio value={'2'}>按体积</Radio>
                                        </RadioGroup>
                                    )}
                                </FormItem>
                                </Form>
                            </Col>
                        </Row>
                </Modal>
            </span>
                )
            }

            // 除指定地区之外的默认运费
            if (this.props.others == '-1') {
                return (
                    <span style={{paddingLeft: 10, paddingRight: 10}}>
                <Button type="primary" onClick={() => this.setModal2Visible(true)}>修改</Button>
                <Modal
                    maskClosable={false}
                    title='修改默认运费'
                    wrapClassName="vertical-center-modal"
                    width='700px'
                    confirmLoading={this.state.confirmLoading}
                    visible={this.state.isVisible}
                    onCancel={() => this.setModal2Visible(false)}
                    onOk={() => this.confirmEdit(true, 3)}
                >
                    <Row>
                            <Col offset={2} span={18}>
                              <Form className="login-form">
                                  {/*设置默认运费*/}
                                  <Col offset={2} className='btnBottom'
                                       style={{fontSize: '16px', color: 'red'}}>除指定地区之外的默认运费</Col>
                                            <FormItem label={this.state.first} {...this.formItemLayout}>
                                                {getFieldDecorator('template_firstNumber', {
                                                    rules: [{required: true, message: '请输入'}],
                                                })(
                                                    <InputNumber min={0} placeholder="请输入"/>
                                                )}
                                            </FormItem>

                                      <FormItem label={'首费'} {...this.formItemLayout}>
                                                {getFieldDecorator('template_firstPrice', {
                                                    rules: [{required: true, message: '请输入首费!'}],
                                                })(
                                                    <InputNumber min={0} precision={2} placeholder="请输入"/>
                                                )}
                                            </FormItem>

                                            <FormItem label={this.state.addOrigin} {...this.formItemLayout}>
                                                {getFieldDecorator('template_lastNumber', {
                                                    rules: [{
                                                        required: true,
                                                        message: '请输入'
                                                    }],
                                                })(
                                                    <InputNumber min={0} placeholder="请输入"/>
                                                )}
                                            </FormItem>

                                            <FormItem label={"续费(元)"} {...this.formItemLayout}>
                                                {getFieldDecorator('template_lastPrice', {
                                                    rules: [{required: true, message: '请输入续费!'}],
                                                })(
                                                    <InputNumber min={0} precision={2} placeholder="请输入"/>
                                                )}
                                            </FormItem>

                                </Form>
                            </Col>
                        </Row>
                </Modal>
            </span>
                )
            }

            // 指定地区的运费
            else {
                return (
                    <span style={{paddingLeft: 10, paddingRight: 10}}>
                <Button type="primary" onClick={() => this.setModal2Visible(true)}>修改</Button>
                <Modal
                    maskClosable={false}
                    title='修改指定地区运费'
                    wrapClassName="vertical-center-modal"
                    width='700px'
                    confirmLoading={this.state.confirmLoading}
                    visible={this.state.isVisible}
                    onCancel={() => this.setModal2Visible(false)}
                    onOk={() => this.confirmEdit(true, 4)}
                >
                    <Row>
                            <Col span={22}>
                              <Form className="login-form">
                                   <FormItem label={"选择地区"} {...this.formDistrictLayout}>
                                            {getFieldDecorator('specialDistrict', {
                                                rules: [{required: true, message: '请选择地区!'}],
                                            })(
                                                <CheckboxGroup options={this.state.provinceLists}
                                                               onChange={(value) => {
                                                                   this.onChangeDistrict(value)
                                                               }}/>
                                            )}
                                        </FormItem>
                                            <FormItem label={this.state.first} {...this.formItemLayout}>
                                                {getFieldDecorator('template_area_firstNumber', {
                                                    rules: [{required: true, message: '请输入'}],
                                                })(
                                                    <InputNumber min={0} placeholder="请输入"/>
                                                )}
                                            </FormItem>

                                     <FormItem label={'首费'} {...this.formItemLayout}>
                                                {getFieldDecorator('template_area_firstPrice', {
                                                    rules: [{required: true, message: '请输入首费!'}],
                                                })(
                                                    <InputNumber min={0} precision={2} placeholder="请输入"/>
                                                )}
                                            </FormItem>

                                            <FormItem label={this.state.addOrigin} {...this.formItemLayout}>
                                                {getFieldDecorator('template_area_lastNumber', {
                                                    rules: [{
                                                        required: true,
                                                        message: '请输入'
                                                    }],
                                                })(
                                                    <InputNumber min={0} placeholder="请输入"/>
                                                )}
                                            </FormItem>

                                            <FormItem label={"续费(元)"} {...this.formItemLayout}>
                                                {getFieldDecorator('template_area_lastPrice', {
                                                    rules: [{required: true, message: '请输入续费!'}],
                                                })(
                                                    <InputNumber min={0} precision={2} placeholder="请输入"/>
                                                )}
                                            </FormItem>

                                </Form>
                            </Col>
                        </Row>
                </Modal>
            </span>
                )
            }
        }

    }

    componentDidMount() {
    }

    componentWillReceiveProps(props) {
        if (props.flag == 1) {
            this.setState({
                isVisible: false
            })
        }
    }

    // 获取选中的地区
    onChangeDistrict(value) {
        this.setState({
            selectProvice: value
        })
    }


    // 弹出关闭框的操作
    setModal2Visible(visible) {
        let provinceList = this.state.provinceLists;  //所有的省
        let others = this.props.others;  //判断是指定地区还是除指定地区
        let parentData = this.props.data;  //编辑所在行的数据
        let provinceCost = this.props.provinceCost; //某个指定地区对应的运费和地址
        let tableAllDistrict = this.props.tableData;  //该模板的所有地区
        let provinceKey = this.props.keys;
        let first;//'首件数（件）',
        let addOrigin; //'续件数（件）',
        let formData = [];
        let disabledCost = [];
        let disabledCostTemp;
        // if (visible) {
        // 运费的单位设置
        if (parentData.template_type == '0') {
            first = '首件数（件）';//'首件数（件）',
            addOrigin = '续件数（件）'; //'续件数（件）',
        }
        if (parentData.template_type == '1') {
            first = '首重（kg）';//'首',
            addOrigin = '续重（kg）'; //'续',
        }
        if (parentData.template_type == '2') {
            first = '首体积（m3）';//'首',
            addOrigin = '续体积（m3）'; //'续',
        }

        // 除指定地区之外的运费的赋值
        if (others == '-1') {
            formData['template_firstNumber'] = {value: parentData.template_firstNumber};  //默认首数量
            formData['template_lastNumber'] = {value: parentData.template_lastNumber};  //默认续数量
            formData['template_firstPrice'] = {value: parentData.template_firstPrice};  //默认首价格
            formData['template_lastPrice'] = {value: parentData.template_lastPrice};  //默认续价格
        }

        // 指定地区的默认运费
        if (others == '1') {
            let specialProvinceString = this.props.specialProvince;  //当前行的地区 (用来控制复选框的勾选)
            let provinceSpecial = [];
            // 当前要编辑的地区，控制复选框的选中
            if ((specialProvinceString.indexOf(',')) > 0) {
                provinceSpecial = specialProvinceString.split(','); //当前编辑行的地区(字符串逗号隔开)
            } else {
                provinceSpecial.push(specialProvinceString);
            }

            let disabledProvince = [];  //即将禁止复选框的地区
            let checkProvince = [];  //把当前编辑行的地区用复选框选中

            // 获取即将禁止的复选框
            tableAllDistrict.forEach((allItem, allIndex) => {
                if (allItem.template_area_key != provinceKey) {
                    disabledProvince.push(allItem.template_area_content);
                    disabledCostTemp = {
                        template_area_content: allItem.template_area_content,   //省
                        template_area_firstNumber: allItem.template_area_firstNumber,   //首数量
                        template_area_lastNumber: allItem.template_area_lastNumber,   //续数量
                        template_area_firstPrice: allItem.template_area_firstPrice,   //	 首价格
                        template_area_lastPrice: allItem.template_area_lastPrice,   // 续价格
                        template_area_key: allItem.template_area_key,   // 前台key值
                    }
                    disabledCost.push(disabledCostTemp);
                }

            });
            provinceList.forEach((provinceItem) => {
                disabledProvince.forEach((disabledItem) => {
                    if (provinceItem.label == disabledItem) {
                        provinceItem.disabled = visible ? true : false;
                    }
                })
            });


            // 把该行的地区选中
            provinceList.forEach((provinceItem, provinceIndex) => {
                provinceSpecial.forEach((specialItem, specialIndex) => {
                    if (provinceItem.label == specialItem) {
                        checkProvince.push(provinceItem.id + ',' + provinceItem.label)
                    }
                })
            })


            formData['specialDistrict'] = {value: checkProvince};
            formData['template_area_firstNumber'] = {value: provinceCost.template_area_firstNumber};  //指定地区首数量
            formData['template_area_lastNumber'] = {value: provinceCost.template_area_lastNumber};  //指定地区续数量
            formData['template_area_firstPrice'] = {value: provinceCost.template_area_firstPrice};  //指定地区首价格
            formData['template_area_lastPrice'] = {value: provinceCost.template_area_lastPrice};  //指定地区续价格
        }

        formData['template_name'] = {value: parentData.template_name};  //模板名称
        formData['template_time'] = {value: parentData.template_time + ''};  //发货时间
        formData['template_method'] = {value: parentData.template_method};  //运送方式
        formData['template_type'] = {value: parentData.template_type};  //计价方式
        this.props.form.setFields(formData);
        // }
        this.setState({
            isVisible: visible,
            first: first,
            provinceLists: provinceList,
            addOrigin: addOrigin,
            disabledCost: disabledCost
        })
    }

    // 提交编辑
    confirmEdit(visible, confirmType) {
        let others = this.props.others;  //判断是指定地区还是除指定地区
        let disabledCost = this.state.disabledCost;  //禁止掉的指定地区的运费
        let parentData = this.props.data;
        let provinceKey = this.props.keys;
        let temp1;
        let tempArray1 = [];
        let tempArray2 = [];
        this.props.form.validateFields((err, values) => {
            if (!err) {
                // 包邮
                if (parentData.template_shipping == 1) {
                    values.template_name = values.template_name;  //	 模板名称
                    values.template_time = values.template_time;  //	发货时间
                } else {
                    // 修改标题和运送方式、计价方式
                    if (others == 0) {
                        values.template_name = values.template_name;  //	 模板名称
                        values.template_time = values.template_time;  //	发货时间
                        values.template_method = values.template_method;  //	运送方式(0,快递 1,EMS 2,平邮)
                        values.template_type = values.template_type;  //	计价方式(0,按件数 1,按重量 2,按体积)
                        values.template_isArea = parentData.template_isArea;  //是否有指定地区(0,否 1,是)
                        values.template_firstNumber = parentData.template_firstNumber;  //默认首数量
                        values.template_lastNumber = parentData.template_lastNumber;  //	默认续数量
                        values.template_firstPrice = parentData.template_firstPrice;  //	默认首价格
                        values.template_lastPrice = parentData.template_lastPrice;  //	默认续价格
                        values.areaList = JSON.stringify(parentData.areaList);
                    }
                    // 除指定地区之外的默认运费
                    else if (others == -1) {
                        values.template_name = parentData.template_name;  //	 模板名称
                        values.template_time = parentData.template_time;  //	发货时间
                        values.template_method = parentData.template_method;  //	运送方式(0,快递 1,EMS 2,平邮)
                        values.template_type = parentData.template_type;  //	计价方式(0,按件数 1,按重量 2,按体积)
                        values.template_isArea = parentData.template_isArea;  //是否有指定地区(0,否 1,是)

                        values.template_firstNumber = values.template_firstNumber;  //默认首数量
                        values.template_lastNumber = values.template_lastNumber;  //	默认续数量
                        values.template_firstPrice = values.template_firstPrice;  //	默认首价格
                        values.template_lastPrice = values.template_lastPrice;  //	默认续价格
                        values.areaList = JSON.stringify(parentData.areaList);
                    }
                    // 只有点击修改指定地区的运费才会进来这里
                    else {
                        values.template_name = parentData.template_name;  //	 模板名称
                        values.template_time = parentData.template_time;  //	发货时间
                        values.template_method = parentData.template_method;  //	运送方式(0,快递 1,EMS 2,平邮)
                        values.template_type = parentData.template_type;  //	计价方式(0,按件数 1,按重量 2,按体积)
                        values.template_isArea = parentData.template_isArea;  //是否有指定地区(0,否 1,是)
                        values.template_firstNumber = parentData.template_firstNumber;  //默认首数量
                        values.template_lastNumber = parentData.template_lastNumber;  //	默认续数量
                        values.template_firstPrice = parentData.template_firstPrice;  //	默认首价格
                        values.template_lastPrice = parentData.template_lastPrice;  //	默认续价格
                        values.specialDistrict.forEach((specialItem) => {
                            let provinceValue = specialItem.split(',');
                            temp1 = {
                                template_area_content: provinceValue[1],   //省
                                template_area_firstNumber: values.template_area_firstNumber,   //首数量
                                template_area_lastNumber: values.template_area_lastNumber,   //续数量
                                template_area_firstPrice: values.template_area_firstPrice,   //首价格
                                template_area_lastPrice: values.template_area_lastPrice,   //续价格
                                template_area_key: provinceKey,   //前台key值
                            }
                            tempArray1.push(temp1);
                        })

                        if (disabledCost.length > 0) {
                            tempArray2 = disabledCost;
                            values.areaList = JSON.stringify((tempArray1.concat(tempArray2)));
                        }
                        if (disabledCost.length == 0) {
                            values.areaList = JSON.stringify(tempArray1);
                        }

                    }
                }


                values.template_id = parentData.template_id;  //	模板ID
                values.template_shipping = parentData.template_shipping;  //	是否包邮(0,不包 1,包邮)
                this.props.clickEdit(values)
            }
        })

    }
}

// EditTransport.propTypes = {
//     others: React.PropTypes.string,
//     provinceCost: React.PropTypes.object,
//     specialProvince: React.PropTypes.string,
//     tableData: React.PropTypes.array,
//     keys: React.PropTypes.string,
//     clickEdit: React.PropTypes.func,
//     flag:React.PropTypes.string,
// }

const EditTransports = Form.create()(EditTransport);
const TransportTemplates = Form.create()(TransportTemplate);
export default TransportTemplates;